﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using FSS.Core.Controls;
using FSS.BLL.Entities;
using FSS.BLL.Services;
using FSS.BLL;
using FSS.Core.Util;

namespace SalesManagement.Controls.List
{
    public partial class EmployeeForm : System.Web.UI.UserControl
    {
        public event EventHandler SaveClick;
        private void OnSaveClick()
        {
            if (SaveClick != null)
            {
                SaveClick(this, EventArgs.Empty);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            BindingStoreBranch();

            BindingStoreDepartment();

            BindingStorePosition();

            BindingStoreRole();
        }

        private void BindingStoreRole()
        {
            RoleService roleS = RoleService.GetInstance();
            IList<RoleEntity> lstRole = roleS.LoadAll();
            if (null != lstRole)
            {
                this.storeRole.DataSource = lstRole;
                this.storeRole.DataBind();
            }
        }

        private void BindingStorePosition()
        {
            PositionService posS = PositionService.GetInstance();
            IList<PositionEntity> lstPos = posS.LoadAll();
            if (null != lstPos)
            {
                this.storePosition.DataSource = lstPos;
                this.storePosition.DataBind();
            }
        }

        private void BindingStoreDepartment()
        {
            DepartmentService departmentS = DepartmentService.GetInstance();
            IList<DepartmentEntity> lstDepartment = departmentS.LoadAll();
            if (null != lstDepartment)
            {
                this.storeDepartment.DataSource = lstDepartment;
                this.storeDepartment.DataBind();
            }
        }

        private void BindingStoreBranch()
        {
            BranchService branchS = BranchService.GetInstance();
            IList<BranchEntity> lstBranch = branchS.LoadAll();
            if (null != lstBranch)
            {
                this.storeBranch.DataSource = lstBranch;
                this.storeBranch.DataBind();
            }
        }
        public void Show()
        {
            this.winDetails.Show();
        }
        public void Show(string empId)
        {
            this.winDetails.Hide();
            ResetWindow();

            EmployeeEntity empE = new EmployeeEntity();
            EmployeeService empS = EmployeeService.GetInstance();
            empE.EmployeeId = int.Parse(empId);
            empE = empS.LoadByPrimaryKey(empE);
            if (null == empE)
                return;
            hdfEmployeeId.Value = empE.EmployeeId;
            txtEmployeeCode.Text = empE.EmployeeCode;
            txtEmployeeName.Text = empE.EmployeeName;
            txtAddress.Text = empE.Address;
            txtPhone.Text = empE.Phone;
            txtMobile.Text = empE.Mobile;
            txtEmail.Text = empE.Email;
            txtFax.Text = empE.Fax;
            txtWebsite.Text = empE.Website;

            txtTaxCode.Text = empE.TaxCode;
            if (empE.BranchId != 0)
                cbbBranch.SetValue(empE.BranchId);
            cbbDepartment.SetValue(empE.DepartmentId);
            cbbPosition.SetValue(empE.PositionId);
            ckbIsActive.Checked = empE.IsActive;

            UserEntity userE = new UserEntity();
            UserService userS = UserService.GetInstance();
            userE.EmployeeId = int.Parse(empId);
            userE = userS.LoadByEmployeeId(userE);
            if (null == userE)
            {
                this.winDetails.Show();
                return;
            }
            hdfUserName.Value = userE.UserName;
            txtUserName.Text = userE.UserName;
            txtUserName.Disabled = true;
            txtPassword.Text = userE.Password;
            txtConfirmPassword.Text = userE.Password;
            cbbRole.SetValue(userE.RoleId);
            ckbIsActive.Checked = userE.IsActive;
            this.winDetails.Show();
        }
        public void ShowAndReset()
        {
            this.winDetails.Hide();
            ResetWindow();
            this.winDetails.Show();
        }

        private void ResetWindow()
        {
            hdfEmployeeId.Value = null;
            hdfUserName.Value = null;
            txtEmployeeCode.Reset();
            txtEmployeeName.Reset();
            txtAddress.Reset();
            txtPhone.Reset();
            txtMobile.Reset();
            txtEmail.Reset();
            txtFax.Reset();
            txtWebsite.Reset();

            txtTaxCode.Reset();
            cbbBranch.Reset();
            cbbDepartment.Reset();
            cbbPosition.Reset();
            ckbIsActive.Checked = true;

            txtUserName.Reset();
            txtUserName.Disabled = false;
            txtPassword.Reset();
            txtConfirmPassword.Reset();
            cbbRole.Reset();
            ckbIsActive.Checked = true;
            this.TabPanel1.SetActiveTab(0);
        }
        protected void btnSave_Click(object sender, DirectEventArgs e)
        {
            EmployeeEntity empE = new EmployeeEntity();
            EmployeeService empS = EmployeeService.GetInstance();
            UserEntity userE = new UserEntity();
            UserService userS = UserService.GetInstance();
            if ("" != hdfEmployeeId.Value.ToString())
            {
                string empId = hdfEmployeeId.Value.ToString();
                empE.EmployeeId = int.Parse(empId);
                empE = empS.LoadByPrimaryKey(empE);
                empE.EmployeeCode = txtEmployeeCode.Text;
                empE.EmployeeName = txtEmployeeName.Text;
                empE.Address = txtAddress.Text;
                empE.Phone = txtPhone.Text;
                empE.Mobile = txtMobile.Text;
                empE.Email = txtEmail.Text;
                empE.Fax = txtFax.Text;
                empE.Website = txtWebsite.Text;

                empE.TaxCode = txtTaxCode.Text;
                if (null != cbbBranch.SelectedItem.Value)
                    empE.BranchId = int.Parse(cbbBranch.SelectedItem.Value);
                empE.DepartmentId = int.Parse(cbbDepartment.SelectedItem.Value);
                empE.PositionId = int.Parse(cbbPosition.SelectedItem.Value);
                empE.IsActive = ckbIsActive.Checked;
                empE.UpdatedDate = DateTime.Now;
                empE.UpdatedBy = FSSContext.Current.User.EmployeeId;
                empS.Update(empE);
            }
            else
            {
                empE.EmployeeCode = txtEmployeeCode.Text;
                empE.EmployeeName = txtEmployeeName.Text;
                empE.Address = txtAddress.Text;
                empE.Phone = txtPhone.Text;
                empE.Mobile = txtMobile.Text;
                empE.Email = txtEmail.Text;
                empE.Fax = txtFax.Text;
                empE.Website = txtWebsite.Text;

                empE.TaxCode = txtTaxCode.Text;
                if (null != cbbBranch.SelectedItem.Value)
                    empE.BranchId = int.Parse(cbbBranch.SelectedItem.Value);
                empE.DepartmentId = int.Parse(cbbDepartment.SelectedItem.Value);
                empE.PositionId = int.Parse(cbbPosition.SelectedItem.Value);
                empE.IsActive = ckbIsActive.Checked;
                empE.CreatedDate = DateTime.Now;
                empE.CreatedBy = FSSContext.Current.User.EmployeeId;
                empE.UpdatedDate = DateTime.Now;
                empE.UpdatedBy = FSSContext.Current.User.EmployeeId;
                empE = empS.InsertExtra(empE);
            }
            if ("" != hdfUserName.Value.ToString() && !string.IsNullOrEmpty(txtUserName.Text))
            {
                userE.UserName = hdfUserName.Value.ToString();
                userE = userS.LoadByPrimaryKey(userE);
                userE.Password = Encryption.StringToMD5Hash(txtPassword.Text);
                userE.RoleId = int.Parse(cbbRole.SelectedItem.Value);
                userE.EmployeeId = empE.EmployeeId;
                userE.IsActive = ckbIsActive.Checked;
                userE.UpdatedDate = DateTime.Now;
                userE.UpdatedBy = FSSContext.Current.User.EmployeeId;
                userS.Update(userE);
            }
            else if ("" == hdfUserName.Value.ToString() && !string.IsNullOrEmpty(txtUserName.Text))
            {
                userE.UserName = txtUserName.Text;
                userE.Password = Encryption.StringToMD5Hash(txtPassword.Text);
                userE.RoleId = int.Parse(cbbRole.SelectedItem.Value);
                userE.EmployeeId = empE.EmployeeId;
                userE.IsActive = ckbIsActive.Checked;
                userE.CreatedDate = DateTime.Now;
                userE.CreatedBy = FSSContext.Current.User.EmployeeId;
                userE.UpdatedDate = DateTime.Now;
                userE.UpdatedBy = FSSContext.Current.User.EmployeeId;
                userS.Insert(userE);
            }
            this.winDetails.Hide();
            OnSaveClick();
        }
    }
}